import { NextResponse } from "next/server"
import {getUserTagsByPage} from "@/server/database/repositories/tag"
import {tagGroupZodFormData,z} from "@/shared/formDataSchema"
import type { NextRequest } from 'next/server'
import { getServerUser } from "@/lib/session"
export async function GET( req: NextRequest) {
  try{
    const paramData: Record<string,any> = {
      page: 1,
      kind: 'expenses'
    }
    const params = req.nextUrl.searchParams
    paramData.kind = params.get('kind')
    paramData.page = Number(params.get('page'))
    tagGroupZodFormData.parse(paramData)

    const user = await getServerUser() as User
    const {id: user_id} = user ?? {}
    const res = await getUserTagsByPage(user_id, paramData.page, paramData.kind)
    return NextResponse.json(res)
  }
  catch(error){
    if (error instanceof z.ZodError) {
      const statusMessage = error.issues.map(issue => issue.message).join("; ");
      return NextResponse.json( statusMessage, { status: 400} )
    }
    return NextResponse.json( '服务器出错，标签数据获取失败！', { status: 500} )
  }
}